package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiT_shipping;
import cn.com.api.service.ApiT_shippingService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "shipping/")
public class ApiT_shippingController {
	@Autowired
	private ApiT_shippingService apiT_shippingService;
	@Autowired
	private CheckService checkService;

	private static Logger logger = Logger
			.getLogger(ApiT_shippingController.class);

	/**
	 * 获取配送员当日有效订单和订单总金额
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getShipperAccount")
	public void getShipperAccount(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String user_id = request.getParameter("user_id");
			Map<String, Object> result_map = apiT_shippingService
					.getShipperAccount(user_id);

			// return
			map.put("datas", result_map);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		//
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();

	}

	/**
	 * 获取配送员配送服务评价
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getShipperShippingRemark")
	public void getShipperShippingRemark(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String user_id = request.getParameter("user_id");
			Map<String, Object> result_map = apiT_shippingService
					.getShipperShippingRemark(user_id);

			// return
			map.put("datas", result_map);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		//
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();

	}

	/**
	 * 我的业绩
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/myAchievement")
	public void myAchievement(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		/**
		 * 需要数据：月累计订单量 、今日新增 、用户评价 月：月累计订单、最快的快递员月订单量、准时送达订单量、超时订单量、预约早达
		 * 平均评分、多少分的评分、1-5星数量
		 */
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String u_id = request.getParameter("u_id");
			String token = request.getParameter("token");
			String date = request.getParameter("date");// 格式：2015-11

			Map<String, String> params = new HashMap<String, String>();
			params.put("u_id", u_id);
			params.put("date", date);
			params.put("token", token);
			if(checkService.checkToken(u_id, token, map)){
				apiT_shippingService.getMyAchievement(params, map);
			}
			
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 * 客户对配送进行评价
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/evaluateShipping")
	public void evaluateShipping(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		try{
			String u_id = request.getParameter("u_id");
			String token = request.getParameter("token");
			String orderId = request.getParameter("orderId");//订单号
			String grade = request.getParameter("grade");// 评价等级 -1,2,3,4,5

			Map<String, String> params = new HashMap<String, String>();
			params.put("u_id", u_id);
			params.put("grade", grade);
			params.put("orderId", orderId);
			if(checkService.checkToken(u_id, token, map)){
				apiT_shippingService.evaluateShipping(params, map);
			}
		}catch(Exception e){
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	/**
	 * 用户每次登录，查询配送服务未评价的订单列表--货已送达未评价
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/getNoEvaluateOrders")
	public void getNoEvaluateOrders(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		try{
			String u_id = request.getParameter("u_id");
			String token = request.getParameter("token");

			Map<String, String> params = new HashMap<String, String>();
			params.put("u_id", u_id);
			if(checkService.checkToken(u_id, token, map)){
				apiT_shippingService.getNoEvaluateOrdersByUser(params, map);
			}
		}catch(Exception e){
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 新增
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "insertT_shipping")
	public void insertT_shipping(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String order_id = request.getParameter("order_id");
		String user_id = request.getParameter("user_id");
		String shipping_price = request.getParameter("shipping_price");
		String status = request.getParameter("status");
		String shipping_remark = request.getParameter("shipping_remark");

		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (order_id != null && !"".equals(order_id.trim())) {
				ApiT_shipping t_shipping = new ApiT_shipping();

				t_shipping.setOrder_id(order_id);
				t_shipping.setUser_id(user_id);
				t_shipping.setShipping_price(Util.getBigDecimal(shipping_price));
				t_shipping.setCreate_time(new Date());

				apiT_shippingService.insertT_shipping(t_shipping);
				// return
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			}
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findT_shippingById")
	public void findT_shippingById(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			ApiT_shipping t_shipping = apiT_shippingService
					.findT_shippingById(Integer.valueOf(id));

			// return
			map.put("datas", t_shipping);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();

	}

	/**
	 * 修改
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "updateT_shipping")
	public void updateT_shipping(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String c_id = request.getParameter("c_id");
		String order_id = request.getParameter("order_id");
		String user_id = request.getParameter("user_id");
		String shipping_price = request.getParameter("shipping_price");
		String status = request.getParameter("status");
		String shipping_remark = request.getParameter("shipping_remark");

		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (c_id != null && !"".equals(c_id.trim())) {
				ApiT_shipping t_shipping = new ApiT_shipping();
				// 获取原数据
				t_shipping = apiT_shippingService.findT_shippingById(Integer
						.valueOf(c_id));
				// 修改
				t_shipping.setOrder_id(order_id);
				t_shipping.setUser_id(user_id);
				t_shipping.setShipping_price(Util.getBigDecimal(shipping_price));
				//
				apiT_shippingService.updateT_shipping(t_shipping);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "deleteT_shipping")
	public void deleteT_shipping(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (id != null && !"".equals(id.trim())) {

				Integer ret_I = apiT_shippingService.deleteT_shipping(Integer
						.parseInt(id));
				if (ret_I > 0) {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "删除成功！");
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
					map.put(Utils.MESSAGE, "检查失败！");
				}
			}
		} catch (Exception e) {
			logger.error(Util.getStackTraceAsString(e));
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
	

}
